package com.user.notiy;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UmsStringUtils {
	public static String NVL(Object source, String replace) {
		if( source == null )
		{
			return replace;
		}else if( source.toString().equals("") )
		{
			return replace;
		}else
		{
			return source.toString();
		}
	}
	
	public static String trim(String source) {
		Pattern pt = Pattern.compile("^\\s*|\\s*$");
		Matcher mt = pt.matcher(source);
		return mt.replaceAll("");
	}
	
	public static boolean isNull(String str) {
		return !isExists(str);
	}
	
	public static boolean isExists(String str) {
		if(str != null && str.length() > 0) {
			return true;
		} else {
			return false;
		}		
	}
	
	/**
	 * @Func	bcd2str
	 * @Brief	BCD转ASCII
	 * @Return	字符串(String)
	 * 
	 * @Param	@Type	@Brief
	 * bytes	byte[]	BCD
	 */
	public static String bcd2str(byte[] bytes) {
		if (bytes == null) {	bytes = "".getBytes();	}
		return bcd2str(bytes, 0, bytes.length);
	}
	
	/**
	 * @Func	bcd2str
	 * @Brief	BCD转ASCII
	 * @Return	字符串(String)
	 * 
	 * @Param	@Type	@Brief
	 * bytes	byte[]	BCD
	 * length	int		长度
	 */
	public static String bcd2str(byte[] bytes, int length) {
		if (bytes == null) {	bytes = "".getBytes();	}
		return bcd2str(bytes, 0, length);
	}
	
	/**
	 * @Func	bcd2str
	 * @Brief	BCD转ASCII
	 * @Return	字符串(String)
	 * 
	 * @Param	@Type	@Brief
	 * bytes	byte[]	BCD
	 * start	int		起始位置
	 * length	int		长度
	 */
	public static String bcd2str(byte[] bytes, int start, int length) {
		int end = 0;
		if(start + length > bytes.length) {
			end = start + bytes.length;
		} else {
			end = start + length;
		}
		
		char temp[] = new char[length * 2];
		char val;
		
		for (int i = start; i < end; i++) {
			val = (char) (((bytes[i] & 0xf0) >> 4) & 0x0f);
			temp[(i - start) * 2] = (char) (val > 9 ? val + 'A' - 10 : val + '0');
			val = (char) (bytes[i] & 0x0f);
			temp[(i - start) * 2 + 1] = (char) (val > 9 ? val + 'A' - 10 : val + '0');
		}
		
		return new String(temp);
	}

	/**
	 * @Func	str2Bcd
	 * @Brief	ASCII转BCD
	 * @Return	BCD(byte[])
	 * 
	 * @Param	@Type	@Brief
	 * asc		String	ASCII
	 */
	public static byte[] str2Bcd(String asc, boolean isLeft) {  
		int len = asc.length();  
		int mod = len % 2;  
		if (mod != 0) {
			if(isLeft) {
				asc = asc + "0";  
				len = asc.length();
			} else {
				asc = "0" + asc;
				len = asc.length();				
			}
		}  
		byte abt[] = new byte[len];  
		if (len >= 2) {  
			len = len / 2;  
		}  
		byte bbt[] = new byte[len];  
		abt = asc.getBytes();  
		int j, k;  
		for (int p = 0; p < asc.length() / 2; p++) {
			if (abt[2 * p] == '=') {
				j = 'D' - 'A' + 0x0a;
			} else if ((abt[2 * p] >= '0') && (abt[2 * p] <= '9')) {  
				j = abt[2 * p] - '0';  
			} else if ((abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) {  
				j = abt[2 * p] - 'a' + 0x0a;  
			} else if ((abt[2 * p] >= 'A') && (abt[2 * p] <= 'Z')) {
				j = abt[2 * p] - 'A' + 0x0a;  
			} else {
				j = 0x00;
			}
			
			if(abt[2 * p + 1] == '=') {
				k = 'D' - 'A' + 0x0a;
			} else if ((abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) {  
				k = abt[2 * p + 1] - '0';  
			} else if ((abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) {
				k = abt[2 * p + 1] - 'a' + 0x0a;  
			} else if ((abt[2 * p + 1] >= 'A') && (abt[2 * p + 1] <= 'Z')) {  
				k = abt[2 * p + 1] - 'A' + 0x0a;  
			} else {
				k = 0x00;
			}
			int a = (j << 4) + k;  
			byte b = (byte) a;  
			bbt[p] = b;  
		}  
		return bbt;  
	}
}
